/**
 * 结算窗口
 */
var Ma_ZongJieSuanWindow = ZongJieSuanWindow.extend({
    /**
     * 给ccsname赋值
     * @protected
     */
    _ccsFileName :res.Ma_ZongJieSuan_json,
    /**
     * 初始化界面
     * @private
     */
    _initInfo: function () {
        var self = this;
        self.fenshu = "";
        self._setWanFa();
        self._setTime();
        self._setRoomId();
        self._setPlayers();
        self._setRowPos();
        self._setInfoText();

        if(typeof activity.gameOverCallback == 'function'){
            activity.gameOverCallback(app.parent_area, gameData.roomId, gameData.uid);
        }
    },
    /**
     * 设置玩法名称
     * @private
     */
    _setWanFa: function () {
        var self = this;
        var wanfa = (self._data['map_id'] ? ('玩法：' + gameData.mapId2Name(self._data['map_id'])) : '玩法：');
        if (this._nodeList["lb_wanfa"]) {
            this._nodeList["lb_wanfa"].setString(wanfa);
        }
    },
    /**
     * 设置时间
     * @private
     */
    _setTime: function () {
        var self = this;
        this._nodeList["lb_ts"].setString(timestamp2time(self._data['ts']));
    },
    /**
     * 设置房间号/局数
     */
    _setRoomId: function () {
        var self = this;
        var roomIdStr = '房号：' + gameData.roomId + '   局数：' + self._data['cur_round'] + '/' + self._data['total_round'];
        this._nodeList["lb_roomid"].setString(roomIdStr);
    },
    /**
     * 设置分享文本
     * @private
     */
    _setInfoText: function () {
        var self = this;
        this._infoText = "";
        var wanfa = '房间号：' + gameData.roomId + (self._data['map_id'] ? ('玩法：' + gameData.mapId2Name(self._data['map_id'])) : '玩法：');
        var endTime = "结束时间：" + timestamp2time(self._data['ts']);
        var maxScore = 0;
        var dyjName = "";
        for (var i = 0; i < self._data.players.length; i++) {
            var player = self._data.players[i];
            if (maxScore < player['score']) {
                maxScore = player['score'];
                var playerInfo = gameData.getPlayerInfoByUid(player.uid);
                dyjName = "大赢家:" + playerInfo.nickname;
            }
        }
        this._infoText = this._infoText + wanfa + "\n" + endTime + "\n" + self.fenshu + dyjName;
    },
    /**
     * 设置玩家数据
     * @private
     */
    _setPlayers: function () {
        var self = this;
        self.maxDianpao = 0;
        self.maxScore = 0;
        self.maxUid = 0;
        var players = self._data.players;
        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            if (self.maxDianpao < player['dianpao']) {
                self.maxDianpao = player['dianpao'];
            }
            if (self.maxScore < player['score']) {
                self.maxScore = player['score'];
            }
        }
        for (var i = 0; i < players.length; i++) {
            var player = players[i];
            var uid = player['uid'];
            var score = player['score'];
            var playerInfo = gameData.getPlayerInfoByUid(uid);
            self.fenshu = self.fenshu + playerInfo.nickname + ":" + score + "\n";
            self._setRowUnit(i, player);
        }
        for (; i < 4; i++) {
            var RowUnit = this._nodeList["row" + i];
            if (RowUnit) {
                RowUnit.setVisible(false);
            }
        }


    },
    /**
     * 设置玩家显示位置
     * @private
     */
    _setRowPos: function () {
        var self = this;
        var count = 1100;
        var midpoint = 0;
        if (this._nodeList['rowWidth']) {
            count = this._nodeList['rowWidth'].getContentSize().width;
            midpoint = this._nodeList['rowWidth'].getPositionX();
        }
        var players = self._data.players;
        var interval = count / players.length;
        var width = this._nodeList['row1'].getPositionX() - this._nodeList['row0'].getPositionX();
        if (players.length == 2) {
            interval = (count - 2 * width) / 3 + width
        }
        var originPosX = midpoint - (players.length - 1) / 2 * interval - width / 2;
        for (var s = 0; s < players.length; s++) {
            this._nodeList['row' + s].setPositionX(originPosX + interval * s);
        }
    },
    /**
     * 设置玩家牌数据
     * @param index
     * @param player
     * @private
     */
    _setRowUnit: function (index, player) {
        var self = this;
        var RowUnit = this._nodeList["row" + index];
        var uid = player.uid;
        var playerInfo = gameData.getPlayerInfoByUid(uid);
        RowUnit["txtName"].setString(ellipsisStr(playerInfo.nickname, 6));
        RowUnit["txtID"].setString("ID:" + uid);
        self._setRowHead(index, playerInfo);
        self._setRowState(index, player);
        self._setScore(index, player);
        self._setScroll(index, player);
        self._setFangzhu(uid, RowUnit);

    },
    /**
     * 设置玩家头像
     * @param index
     * @param playerInfo
     * @private
     */
    _setRowHead: function (index, playerInfo) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var url = decodeURIComponent(playerInfo.headimgurl);
        if (url == undefined || (url.length == 0)) url = res.defaultHead;
        loadImageToSprite(url, RowUnit["head"]);//头像
    },
    /**
     * 设置玩家房主
     * @param index
     * @param playerInfo
     * @private
     */
    _setFangzhu: function (uid,RowUnit) {

    },
    /**
     * 设置状态
     * @param index
     * @param playerData
     * @private
     */
    _setRowState: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var score = playerData['score'];
        RowUnit["dyj"].setVisible(self.maxScore <= score && score > 0);
    },
    /**
     * 设置列表
     * @param index
     * @param playerData
     * @private
     */
    _setScroll: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var sv = RowUnit["sv"];
        var row0 = sv["row0"];
        row0.setVisible(false);
        var totalheight = row0.getContentSize().height * 5;
        if (totalheight < sv.getContentSize().height)
            totalheight = sv.getContentSize().height;
        sv.setInnerContainerSize(cc.size(sv.getContentSize().width, totalheight));
        var tab1 = ["自摸次数:", "胡牌次数:", "点炮次数:", "暗杠次数:", "明杠次数:"];
        var tab2 = ["zimo", "hu", "dianpao", "angang", "minggang"];
        for (var j = 0; j < 5; j++) {
            var row = sv["row" + j];
            if (!row) {
                row = duplicateNode(row0);
                row.setName('row' + j);
                sv.addChild(row);
            }
            row.setVisible(true);
            row.setPosition(0, totalheight - j * row0.getContentSize().height);
            if (tab2[j] == "hu") {
                var hupaicishu = playerData["zimo"] + playerData["jiepao"];
                row.getChildByName("txt_jushu").setString(tab1[j]);
                row.getChildByName("txt_score").setString(hupaicishu);
            }
            else {
                row.getChildByName("txt_jushu").setString(tab1[j]);
                row.getChildByName("txt_score").setString(playerData[tab2[j]]);
            }
            row.getChildByName("txt_jushu").setLocalZOrder(2);
            row.getChildByName("txt_score").setLocalZOrder(2);
        }
    },
    /**
     * 设置分数
     * @param index
     * @param playerData
     * @private
     */
    _setScore: function (index, playerData) {
        var self = this;
        var RowUnit = self._nodeList["row" + index];
        var score = playerData['score'];
        RowUnit["lb_score_win"].setString(score);
        RowUnit["lb_score_fail"].setString(score);
        RowUnit["lb_score_win"].setVisible(score >= 0);
        RowUnit["lb_score_fail"].setVisible(score < 0);
    }
});